#!/bin/bash
# USAGE:
#	undo processName [fileName]
#	undo processName
# 	undo only processName
#
# DESCRIPTION:
#  UNDO is an internal ADORE command. It removes the 
#  given processName from the result files.   
#
# INPUT:
#  only: if specified undo removes only a single step and keep 
#    the information for the following steps. Use with caution.
#  processName: Doris step to be deleted from the resultfile.
#  
#  Optional:
#    fileName: If omitted undo will try to remove the given doris
#      step from master, slave and interferograms result files.    
#      If specified undo will only remove steps from the given file.
#
# OUTPUT:
#  Undo Successful: if DORIS step was found and removed.
#  

# Updates:
# Modified 2010-03-10 Added ONLY option to remove a single process.
# Modified 2012-12-21 Added support for multiple process names with only. 

dorisProcessReset()
{
## AUTHOR:   Mahmut Arikan
## EMAIL:    M.Arikan@TUDelft.nl
## VERSION:  v1.4
## DATE:     20060612
## UPDATE:   20090114
##
## TUDelft, DEOS Radar Group  - 2006
##
##
## reset doris steps in the result files,
## up to the processing step you want
##

 if [ -e "${2}" ]; then
    
   lineno=$(awk '/_Start_'$1'/{print NR-1}' $2);  # get me the line number of the process line - 1
    
   if [ "$lineno" != "" ]; then 
     echo "  ${1} found at ${lineno} in ${2}"
     if [ "${3}" == "only" ]; then
       linenoEnd=$(awk '/End_'$1'/{print NR+1}' $2);  #get the line number for the end of step.
       sed -i -e '/^'$1'/s/1/0/g' -e ''${lineno},${linenoEnd}' d' $2
       [ $? -eq 0 ] && echo "  Undo Successful."
     else
       sed -i -e '/^'$1'/,/End_process_control/s/1/0/g' -e ''${lineno}',$ d' $2
       [ $? -eq 0 ] && echo "  Undo Successful."
     fi
   else
     error "  ${1} not found in ${2}. This might not be important."
   fi
 else
   error "  ${2} does not exist. This might not be important."
 fi
}

local dorisStep only resfile user_resfile process
if [ "${1}" == "only" ];then
  shift
  only="only"
else
  only=""
fi

[ -e "${@: -1}" ] && user_resfile="${@: -1}" || user_resfile=""

for process in ${@}
do 
  [ "${process}" == "${user_resfile}" ] && continue # if last parameter, skip.
  # dorisStep=`pn2rs ${1}`
  
  # addrefpha2s_crop is different. 
  if [ "${process}" == "addrefpha2s_crop" ]; then
    # Check if the s_resfile is in the current folder
    if [ -e "${s_resfile}.bck" ]; then
      cp ${s_resfile}.bck ${s_resfile} # replace with backup (original) slave result file.
    #if not check if it may be in the cropFolder (batch processing)
    elif [ -e "${cropFolder}/${slave}/$(basename ${s_resfile}).bck" ]; then
      cp ${cropFolder}/${slave}/$(basename ${s_resfile}).bck ${cropFolder}/${slave}/$(basename ${s_resfile}) # replace with backup (original) slave result file.
    else
      # if no backup file skip.
      error "File not found: ${s_resfile}.bck"
      continue
    fi
  fi
  # standard undo
  dorisStep=`pn2rs ${process}` # convert process name to result file step
  if [ "${user_resfile:-undefined}" != "undefined" ]; then
    dorisProcessReset ${dorisStep} ${user_resfile} ${only};
  elif [[ "${process}" == m_* ]]; then
    dorisProcessReset ${dorisStep} ${m_resfile} ${only};
  elif [[ "${process}" == s_* ]]; then
    dorisProcessReset ${dorisStep} ${s_resfile} ${only}; 
  else # Lets see if pn2rs knows which result file.
    resfile=`pn2rs -f ${process}`
    if [ -e "${resfile}" ]; then 
      dorisProcessReset ${dorisStep} "${resfile}" ${only}
    else # I don't know what file so I will try all
      dorisProcessReset ${dorisStep} ${m_resfile} ${only}
      dorisProcessReset ${dorisStep} ${s_resfile} ${only}
      dorisProcessReset ${dorisStep} ${i_resfile} ${only}
    fi
  fi
done
# cleanup
unset user_resfile dorisStep only resfile dorisProcessReset process

